<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="title">
        <img src="../assets/ERP.png" alt="">
        <p>电商后台管理系统</p>
      </div>
      <el-button type="info" @click="exit">安全退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px': '200px'">
        <!-- 侧边栏 -->
        <div class="menuButton" @click="handleShow">|||</div>
        <!-- 菜单 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" 
        :unique-opened='true' :collapse="isCollapse" :collapse-transition="false" 
        :router="true" :default-active="nowActiveIndex">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="(item, index) in menulist" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <i :class="iconList[index]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" 
            :key="subItem.id" @click="headleSetActive(subItem.path)">
              <template>
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>    
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <!-- 内容区 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      isCollapse: false,
      // 当前激活的菜单下标
      nowActiveIndex: '',
      iconList: ['iconfont icon-yonghu', 'iconfont icon-quanxian', 'iconfont icon-shangpin', 'iconfont icon-icon-', 'iconfont icon-shujutongji']
    }
  },
  created() {
    this.$http.get('menus').then(res => {
      // console.log(res.data)
      if(res.data.meta.status !== 200){
        return this.$message.error(res.data.meta.msg)
      }else {
        this.menulist = res.data.data
      }
    })
  },  
  mounted() {
    this.headleGetActive()
  },
  methods: {
    exit(){
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 点击按钮切换菜单折叠和展开
    handleShow () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    headleSetActive(activePath) {
      window.sessionStorage.setItem('menuActive', '/' + activePath)
    },
    headleGetActive() {
      this.nowActiveIndex = window.sessionStorage.getItem('menuActive')
    }
  },
}
</script>
<style lang="scss" scoped>
  .iconfont {
    margin-right: 10px;
  }
  .el-container {
    height: 100%;  
    .el-header {
      background-color: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding-left: 0;
      .title {
        display: flex;
        align-items: center;
        width: 400px;
        img {
          margin-left: 10px;
          height: 32px;
          width: 32px;
        }
        p {
          margin-left: 10px;
          display: inline-block;
          font-size: 24px;
          line-height: 10px;
          color: white;
        }
      }
    }
    .el-aside {
      .el-menu {
        border-right: none;
      }
      height: 100%;
      background-color: #333744;
    }
    .el-main {
      background-color: #eaedf1;
    }
  }
  .menuButton {
    color: white;
    background-color: #4a5064;
    text-align: center;
    line-height: 24px;
    font-size: 10px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>